$(document).ready(function() {
	var __groupForm = $('#form_group_add'),
		__editForm = $('#form_edit');


	$('#submit_form').click(function() {
		var objAuthForm = {
			__formName: $("input[name='name']", __groupForm),
			__formLevel: $("input[name='level']", __groupForm),
			__formAuthList: $("input[name='auth_list']", __groupForm)
		}

		var arr_auth_list = [];

		$("input[name='authority_id']:checked", __groupForm).each(function() {
			arr_auth_list.push($(this).val());
		});


		for (var i in objAuthForm) {
			if (objAuthForm[i].val() == '') {
				objAuthForm[i].css({
					'border-color': 'red'
				});
				return false;
			} else {
				objAuthForm[i].css({
					'border-color': '#e5e5e5'
				});
			}
		}

		if (arr_auth_list.length == 0) {
			alert('请至少选择一条权限！');
			return false;
		}

		$.ajax({
			type: "POST",
			url: "/Group/add",
			data: {
				"name": objAuthForm.__formName.val(),
				"level": 　objAuthForm.__formLevel.val(),
				"authority_id": arr_auth_list
			},
			success: function(data) {
				//window.alert(data.info);
				//if (true === data.status) window.location = "/Group/list";
				layer.msg(data.info);
				if (true === data.status) {
					window.location = "/Group/list";
				};
			}
		});

	});

	//$('.groupDelete').click(function(){
	$(document).on('click', '.tableDelete', function() {
		var userId = $(this).parent('td').attr('item_id'),
			thisUrl = $(this).attr('href');
		//官网欢迎页   
		layer.confirm('确认删除', {
			btn: ['确认', '取消'] //按钮
		}, function(index) {

			$.ajax({
				type: "GET",
				url: thisUrl,
				async: true,
				data: {
					//"id" : userId
				},
				success: function(data) {
					layer.msg(data.info);

					if (true === data.status) {
						window.location = "/Group/list";
					}

				}
			});
			layer.close(index);
			return false;

		}, function(index) {
			layer.close(index);
		});
		return false;
	});


	//$('.groupEdit').click(function(e){
	$(document).on('click', '.tableEdit', function(e) {
		$('#form_group_edit').modal("toggle");
		var urlOld = $(this).attr('href'),
			urlArr = urlOld.split("/");
		urlId = urlArr[urlArr.length - 1];

		var __parent = $(this).parent('td').parent('tr.gradeX'),
			__formName = $("input[name='name']", __editForm),
			__formLevel = $("input[name='level']", __editForm);
		var tdTable = [];
		$('td', __parent).each(function() {
			tdTable.push($(this).text());
		});
		__formName.val(tdTable[0]);
		__formLevel.val(tdTable[1]);

		var _formAuthList = $('#authList');
		$('label.checkbox div.checker span', _formAuthList).removeClass('checked');
		var _inputAuthList = $('label.checkbox div.checker span input', _formAuthList);

		var obj = group_authority_list;

		//alert(111);
		//alert(_inputAuthList);
		//
		//alert(obj);
		$.each(obj, function(k, v) {
			if (k == "group_id_" + urlId) {
				//alert(k);
				$.each(v, function(kk, vv) {
					$.each(vv, function(kkk, vvv) {
						for (var i in vvv) {
							if (vvv[i] == urlId) continue;
							_inputAuthList.each(function() {
								if ($(this).val() == i) {
									$(this).parent('span').addClass('checked');
								}
							});

						}
					});
				});
			}
		});



		return false;
	});


	$('#edit_form').click(function() {
		var objGroupForm = {
			__formName: $("input[name='name']", __editForm),
			__formLevel: $("input[name='level']", __editForm)
		}
		for (var i in objGroupForm) {

			if (objGroupForm[i].val() == '') {
				objGroupForm[i].css({
					'border-color': 'red'
				});
				return false;
			} else {
				objGroupForm[i].css({
					'border-color': '#e5e5e5'
				});
			}
		}
		var arrAuthList = [];
		//span.checked
		$("input[name='authority_id']:checked", __editForm).each(function() {
			arrAuthList.push($(this).val());
		});


		$.ajax({
			type: "POST",
			url: "/Group/edit/id/" + urlId,
			data: {
				"name": objGroupForm.__formName.val(),
				"level": 　objGroupForm.__formLevel.val(),
				"authority_id": arrAuthList
			},
			success: function(data) {
				layer.msg(data.info);

				if (true === data.status) {
					window.location = "/Group/list";
				}

			}
		});

		return false;

	});

	//var nextBtn = $('#page_next');
	$(document).on('click', '#page_next,#page_prev,#page_index,#page_end', function() {
		var urlOld = $(this).attr('href'),
			urlArr = urlOld.split("/");
		urlId = urlArr[urlArr.length - 1];
		$.ajax({
			type: 'POST',
			url: '/Group/page/page/' + urlId,
			data: urlId,
			success: function(data) {
				var data = $.parseJSON(data);

				$('#tbody').html('');
				var id = '';

				$.each(data, function(k, v) {

					id = v['group_id'];


					$('#tbody').append($('<tr class="gradeX"><td>' + v['name'] + '</td><td>' + v['level'] + '</td><td ><a class="tableDelete" href="/Group/del/id/' + id + '">删除</a> / <a class="tableEdit" href="/Group/edit/id/' + id + '">修改</a> </td></tr>'));

				});


			}

		});
		$.ajax({
			type: 'POST',
			url: '/Group/currentPage/page/' + urlId,
			data: urlId,
			success: function(data) {
				console.log(data);
				$('#pages').html(data);
				$('#pages').ready(function() {
					nextBtn = $('#page_next');
				});
			}

		});
		return false;
	});

});